package com.wangsh.userscenter.back.controller;

import java.net.URLEncoder;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.wangsh.userscenter.auth.pojo.AMenu;
import com.wangsh.userscenter.auth.pojo.ARoleMenu;
import com.wangsh.userscenter.auth.service.IAuthService;
import com.wangsh.userscenter.common.controller.BaseController;
import com.wangsh.userscenter.common.util.ConstatFinalUtil;
import com.wangsh.userscenter.common.util.PageInfoUtil;
import com.wangsh.userscenter.users.pojo.AAdmins;
import com.wangsh.userscenter.users.pojo.ARole;
import com.wangsh.userscenter.users.service.IUsersOperService;
import com.wangsh.userscenter.users.service.IUsersService;

/**
 * 登陆后的管理员操作
 * 
 * url规划:凡是带/back的必须是登陆后才能操作
 * 
 * @author WangshSxt
 *
 */
@Controller
@RequestMapping("/back/admins")
public class AdminsBackController extends BaseController
{
	@Resource
	private IUsersService usersService;
	@Resource
	private IAuthService authService;
	@Resource
	private IUsersOperService usersOperService;
	
	/**
	 * 登陆后的首页
	 * @return
	 */
	@RequestMapping("/main")
	public String main()
	{
		ConstatFinalUtil.SYS_LOGGER.info("--main--");
		return "/back/admins/main";
	}
	
	/**
	 * 登陆后的首页
	 * @return
	 */
	@RequestMapping("/leftMenu")
	public String leftMenu()
	{
		ConstatFinalUtil.SYS_LOGGER.info("--leftMenu--");
		return "/back/admins/leftMenu";
	}
	
	/*---- 管理员模块开始 ----*/
	/**
	 * 管理员列表
	 * @return
	 */
	@RequestMapping("/adminsList")
	public String adminsList(String keyword,String status,String st,String ed,Model model,
			String currentPage,String pageSize)
	{
		ConstatFinalUtil.SYS_LOGGER.info("==adminsList==");
		/* 将字符串变成时间
		 * */
		if(st == null)
		{
			st = "" ; 
		}
		if(ed == null)
		{
			ed = "" ; 
		}
		if(status == null)
		{
			status = "" ; 
		}
		
		/* 搜索条件 */
		Map<String, Object> condMap = new HashMap<String, Object>();
		condMap.put("keyword", keyword);
		if(!"".equalsIgnoreCase(status))
		{
			condMap.put("status", status);
		}
		
		Date stDate = null ; 
		Date edDate = null ;
		if(!"".equalsIgnoreCase(st) && !"".equalsIgnoreCase(ed))
		{
			/*将字符串转换成日期
			 * 
			 * 完善了DateFormatUtil
			 * */
			stDate = this.dateFormatUtil.strDateTime(st); 
			edDate = this.dateFormatUtil.strDateTime(ed); ; 
		}
		condMap.put("stDate", stDate);
		condMap.put("edDate", edDate);
		
		PageInfoUtil pageInfoUtil = proccedPageInfo(currentPage, pageSize);
		
		/* 查询结果 */
		Map resultMap = this.usersService.findCondListAdminsService(pageInfoUtil,condMap);
		List<AAdmins> adminsList = (List<AAdmins>) resultMap.get("list");
		pageInfoUtil = (PageInfoUtil) resultMap.get("pageInfoUtil");
		/* 将结果放到四大作用域中 */
		model.addAttribute("adminsList", adminsList);
		model.addAttribute("pageInfoUtil", pageInfoUtil);
		
		/* 将查询条件放到request中 */
		model.addAttribute("keyword", keyword);
		model.addAttribute("status", status);
		model.addAttribute("st", st);
		model.addAttribute("ed", ed);
		return "/back/admins/adminsList";
	}
	
	/**
	 * 打开管理员添加页面
	 * @return
	 */
	@RequestMapping("/adminsInsert")
	public String adminsInsert(Model model)
	{
		ConstatFinalUtil.SYS_LOGGER.info("--adminsInsert--");
		
		/* 查询所有启用的角色 */
		Map<String, Object> condMap = new HashMap<String, Object>();
		condMap.put("status", "1");
		Map<String, Object> resultMap = this.usersService.findCondListRoleService(null, condMap);
		List<ARole> roleList = (List<ARole>) resultMap.get("list");
		model.addAttribute("roleList", roleList);
		return "/back/admins/adminsInsert"; 
	}
	
	/**
	 * 打开管理员添加提交的操作
	 * @return
	 * 返回结果:为json
	 * {
	 * 	"statusCode":"200", 
	 * 	"message":"成功", 
	 * 	"navTabId":"", 
	 * 	"rel":"", 
	 * 	"callbackType":"", 
	 * 	"forwardUrl":"", 
	 * 	"confirmMsg":"" 
	 *	}
	 */
	@RequestMapping(value="/adminsInsertSubmit",produces="text/html;charset=UTF-8")
	@ResponseBody
	public String adminsInsertSubmit(HttpServletRequest request,AAdmins admins)
	{
		ConstatFinalUtil.SYS_LOGGER.info("--adminsInsertSubmit--");
		/* 用户不能填写的,后台手动赋值 */
		/* 密码 */
		admins.setPassword(this.encryptUtil.encodeStr(admins.getPassword()));
		
		/* 时间 */
		admins.setCreateTime(new Date());
		admins.setUpdateTime(new Date());
		admins.setLastLoginTime(new Date());
		
		/* 存储数据 */
		JSONObject resDbJSON = this.usersService.saveOneAdminsService(admins);
		/* 结果 */
		this.info = resDbJSON.get("info") + "" ; 
		
		JSONObject resultJSON = printJuiJson(request,info);
		return resultJSON.toJSONString(); 
	}
	
	/**
	 * 打开管理员更新的页面
	 * 
	 * 查询你要更新的管理员
	 * 
	 * @return
	 */
	@RequestMapping("/adminsUpdate")
	public String adminsUpdate(HttpServletRequest request,String operType , String self,String aid,Model model)
	{
		ConstatFinalUtil.SYS_LOGGER.info("--adminsUpdate--");
		
		Map<String, Object> condMap = new HashMap<String, Object>();
		/* 根据Id查询管理员 */
		condMap.put("id", aid);
		AAdmins admins = this.usersService.findOneAdminsService(condMap);
		
		/* 用户自己修改 */
		if("true".equalsIgnoreCase(self))
		{
			admins = (AAdmins) this.findObjfromSession(request, "2");
		}
		model.addAttribute("admins", admins);
		
		if("updateInfo".equalsIgnoreCase(operType))
		{
			/* 查询所有启用的角色 */
			condMap.clear();
			condMap.put("status", "1");
			Map<String, Object> resultMap = this.usersService.findCondListRoleService(null, condMap);
			List<ARole> roleList = (List<ARole>) resultMap.get("list");
			model.addAttribute("roleList", roleList);
			return "/back/admins/adminsUpdate";
		}else if("updatePass".equalsIgnoreCase(operType))
		{
			return "/back/admins/adminsUpdatePass";
		}else if("resetPass".equalsIgnoreCase(operType))
		{
			return "/back/admins/adminsResetPass";
		}
		return "/back/admins/adminsInfo";
	}
	
	/**
	 * 管理员更新提交
	 * @return
	 */
	@RequestMapping(value = "/adminsUpdateSubmit",produces="text/html;charset=UTF-8")
	@ResponseBody
	public String adminsUpdateSubmit(String operType,String souPass,HttpServletRequest request,AAdmins admins)
	{
		ConstatFinalUtil.SYS_LOGGER.info("--adminsUpdateSubmit--");
		/* 根据id从数据库中再查询一次 
		 * 一一的赋值*/
		Map<String, Object> condMap = new HashMap<String, Object>();
		/* 根据Id查询管理员 */
		condMap.put("id", admins.getId());
		AAdmins adminsRes = this.usersService.findOneAdminsService(condMap);
		
		boolean flag = false ; 
		if("updateInfo".equalsIgnoreCase(operType))
		{
			adminsRes.setRoleId(admins.getRoleId());
			adminsRes.setEmail(admins.getEmail());
			adminsRes.setName(admins.getName());
			adminsRes.setPhone(admins.getPhone());
			adminsRes.setQq(admins.getQq());
			adminsRes.setSex(admins.getSex());
			adminsRes.setStatus(admins.getStatus());
			
			flag = true ; 
		}else if("updatePass".equalsIgnoreCase(operType))
		{
			/* 比较原密码 */
			/* 检查源密码是否正确 */
			if(this.encryptUtil.checkStr(souPass, adminsRes.getPassword()))
			{
				 /* 密码正确 */
				adminsRes.setPassword(this.encryptUtil.encodeStr(admins.getPassword()));
				flag = true ; 
			}else
			{
				/* 密码不正确 */
				this.info = ConstatFinalUtil.INFO_JSON.getString("21") ;
			}
		}else if("resetPass".equalsIgnoreCase(operType))
		{
			 /* 密码正确 */
			adminsRes.setPassword(this.encryptUtil.encodeStr(admins.getPassword()));
			flag = true ; 
		}
		
		if(flag)
		{
			adminsRes.setUpdateTime(new Date());
			/* 千万千万别使用Spring的对象驱动;在Controller方法中增加一个对象 */
			JSONObject resDbJSON = this.usersService.updateOneAdminsService(adminsRes);
			this.info = resDbJSON.getString("info");
		}
		JSONObject resultJSON = this.printJuiJson(request, info);
		return resultJSON.toJSONString(); 
	}
	/*---- 管理员模块结束 ----*/
	
	/*---- 角色模块开始 ----*/
	/**
	 * 角色列表
	 * @return
	 */
	@RequestMapping("/roleList")
	public String roleList(String keyword,String status,String st,String ed,Model model,
			String currentPage,String pageSize)
	{
		ConstatFinalUtil.SYS_LOGGER.info("==roleList==");
		/* 将字符串变成时间
		 * */
		if(st == null)
		{
			st = "" ; 
		}
		if(ed == null)
		{
			ed = "" ; 
		}
		if(status == null)
		{
			status = "" ; 
		}
		
		/* 搜索条件 */
		Map<String, Object> condMap = new HashMap<String, Object>();
		condMap.put("keyword", keyword);
		if(!"".equalsIgnoreCase(status))
		{
			condMap.put("status", status);
		}
		
		Date stDate = null ; 
		Date edDate = null ;
		if(!"".equalsIgnoreCase(st) && !"".equalsIgnoreCase(ed))
		{
			/*将字符串转换成日期
			 * 
			 * 完善了DateFormatUtil
			 * */
			stDate = this.dateFormatUtil.strDateTime(st); 
			edDate = this.dateFormatUtil.strDateTime(ed); ; 
		}
		condMap.put("stDate", stDate);
		condMap.put("edDate", edDate);
		
		PageInfoUtil pageInfoUtil = proccedPageInfo(currentPage, pageSize);
		
		/* 查询结果 */
		Map resultMap = this.usersService.findCondListRoleService(pageInfoUtil,condMap);
		List<ARole> roleList = (List<ARole>) resultMap.get("list");
		pageInfoUtil = (PageInfoUtil) resultMap.get("pageInfoUtil");
		/* 将结果放到四大作用域中 */
		model.addAttribute("roleList", roleList);
		model.addAttribute("pageInfoUtil", pageInfoUtil);
		
		/* 将查询条件放到request中 */
		model.addAttribute("keyword", keyword);
		model.addAttribute("status", status);
		model.addAttribute("st", st);
		model.addAttribute("ed", ed);
		return "/back/admins/roleList";
	}
	
	/**
	 * 打开角色添加页面
	 * @return
	 */
	@RequestMapping("/roleInsert")
	public String roleInsert(Model model)
	{
		ConstatFinalUtil.SYS_LOGGER.info("--roleInsert--");
		return "/back/admins/roleInsert"; 
	}
	
	/**
	 * 打开角色添加提交的操作
	 * @return
	 * 返回结果:为json
	 * {
	 * 	"statusCode":"200", 
	 * 	"message":"成功", 
	 * 	"navTabId":"", 
	 * 	"rel":"", 
	 * 	"callbackType":"", 
	 * 	"forwardUrl":"", 
	 * 	"confirmMsg":"" 
	 *	}
	 */
	@RequestMapping(value="/roleInsertSubmit",produces="text/html;charset=UTF-8")
	@ResponseBody
	public String roleInsertSubmit(HttpServletRequest request,ARole role,String pubTimeStr)
	{
		ConstatFinalUtil.SYS_LOGGER.info("--roleInsertSubmit--");
		AAdmins adminsSess = (AAdmins) this.findObjfromSession(request, "2");
		
		/* 设置发布时间 */
		role.setPubTime(this.dateFormatUtil.strDateTime(pubTimeStr));
		/* 时间 */
		role.setCreateId(adminsSess.getId());
		role.setCreateTime(new Date());
		role.setUpdateTime(new Date());
		
		/* 存储数据 */
		JSONObject resDbJSON = this.usersService.saveOneRoleService(role);
		/* 结果 */
		this.info = resDbJSON.get("info") + "" ; 
		
		JSONObject resultJSON = printJuiJson(request,info);
		return resultJSON.toJSONString(); 
	}
	
	/**
	 * 打开角色更新的页面
	 * 
	 * 查询你要更新的角色
	 * 
	 * @return
	 */
	@RequestMapping("/roleUpdate")
	public String roleUpdate(HttpServletRequest request,String operType , String self,String rid,Model model)
	{
		ConstatFinalUtil.SYS_LOGGER.info("--roleUpdate--");
		
		Map<String, Object> condMap = new HashMap<String, Object>();
		/* 根据Id查询角色 */
		condMap.put("id", rid);
		ARole role = this.usersService.findOneRoleService(condMap);
		
		/* 用户自己修改 */
		if("true".equalsIgnoreCase(self))
		{
			role = (ARole) this.findObjfromSession(request, "2");
		}
		model.addAttribute("role", role);
		
		if("updateInfo".equalsIgnoreCase(operType))
		{
			return "/back/admins/roleUpdate";
		}
		return "/back/admins/roleInfo";
	}
	
	/**
	 * 角色更新提交
	 * @return
	 */
	@RequestMapping(value = "/roleUpdateSubmit",produces="text/html;charset=UTF-8")
	@ResponseBody
	public String roleUpdateSubmit(HttpServletRequest request,ARole role,String operType,String pubTimeStr)
	{
		ConstatFinalUtil.SYS_LOGGER.info("--roleUpdateSubmit--");
		AAdmins adminsSess = (AAdmins) this.findObjfromSession(request, "2");
		/* 根据id从数据库中再查询一次 
		 * 一一的赋值*/
		Map<String, Object> condMap = new HashMap<String, Object>();
		/* 根据Id查询角色 */
		condMap.put("id", role.getId());
		ARole roleRes = this.usersService.findOneRoleService(condMap);
		
		boolean flag = false ; 
		if("updateInfo".equalsIgnoreCase(operType))
		{
			roleRes.setPubTime(this.dateFormatUtil.strDateTime(pubTimeStr));
			
			roleRes.setName(role.getName());
			roleRes.setStatus(role.getStatus());
			roleRes.setContent(role.getContent());
			roleRes.setUpdateTime(new Date());
			
			roleRes.setUpdateId(adminsSess.getId());
			
			flag = true ; 
		}
		
		if(flag)
		{
			roleRes.setUpdateTime(new Date());
			/* 千万千万别使用Spring的对象驱动;在Controller方法中增加一个对象 */
			JSONObject resDbJSON = this.usersService.updateOneRoleService(roleRes);
			this.info = resDbJSON.getString("info");
		}
		JSONObject resultJSON = this.printJuiJson(request, info);
		return resultJSON.toJSONString(); 
	}
	/*---- 角色模块结束 ----*/
	
	/*---- 菜单模块开始 ----*/
	/**
	 * 菜单列表
	 * @return
	 */
	@RequestMapping("/menuList")
	public String menuList(String keyword,String status,String st,String ed,Model model,
			String currentPage,String pageSize)
	{
		ConstatFinalUtil.SYS_LOGGER.info("==menuList==");
		/* 将字符串变成时间
		 * */
		if(st == null)
		{
			st = "" ; 
		}
		if(ed == null)
		{
			ed = "" ; 
		}
		if(status == null)
		{
			status = "" ; 
		}
		
		/* 搜索条件 */
		Map<String, Object> condMap = new HashMap<String, Object>();
		condMap.put("keyword", keyword);
		if(!"".equalsIgnoreCase(status))
		{
			condMap.put("status", status);
		}
		
		Date stDate = null ; 
		Date edDate = null ;
		if(!"".equalsIgnoreCase(st) && !"".equalsIgnoreCase(ed))
		{
			/*将字符串转换成日期
			 * 
			 * 完善了DateFormatUtil
			 * */
			stDate = this.dateFormatUtil.strDateTime(st); 
			edDate = this.dateFormatUtil.strDateTime(ed); ; 
		}
		condMap.put("stDate", stDate);
		condMap.put("edDate", edDate);
		
		PageInfoUtil pageInfoUtil = proccedPageInfo(currentPage, pageSize);
		
		/* 查询结果 */
		Map resultMap = this.authService.findCondListMenuService(pageInfoUtil,condMap);
		List<AMenu> menuList = (List<AMenu>) resultMap.get("list");
		pageInfoUtil = (PageInfoUtil) resultMap.get("pageInfoUtil");
		/* 将结果放到四大作用域中 */
		model.addAttribute("menuList", menuList);
		model.addAttribute("pageInfoUtil", pageInfoUtil);
		
		/* 将查询条件放到request中 */
		model.addAttribute("keyword", keyword);
		model.addAttribute("status", status);
		model.addAttribute("st", st);
		model.addAttribute("ed", ed);
		return "/back/admins/menuList";
	}
	
	/**
	 * 打开菜单添加页面
	 * @return
	 */
	@RequestMapping("/menuInsert")
	public String menuInsert(Model model)
	{
		ConstatFinalUtil.SYS_LOGGER.info("--menuInsert--");
		return "/back/admins/menuInsert"; 
	}
	
	/**
	 * 打开菜单添加提交的操作
	 * @return
	 * 返回结果:为json
	 * {
	 * 	"statusCode":"200", 
	 * 	"message":"成功", 
	 * 	"navTabId":"", 
	 * 	"rel":"", 
	 * 	"callbackType":"", 
	 * 	"forwardUrl":"", 
	 * 	"confirmMsg":"" 
	 *	}
	 */
	@RequestMapping(value="/menuInsertSubmit",produces="text/html;charset=UTF-8")
	@ResponseBody
	public String menuInsertSubmit(HttpServletRequest request,AMenu menu,String pubTimeStr)
	{
		ConstatFinalUtil.SYS_LOGGER.info("--menuInsertSubmit--");
		AAdmins adminsSess = (AAdmins) this.findObjfromSession(request, "2");
		
		/* 设置发布时间 */
		menu.setPubTime(this.dateFormatUtil.strDateTime(pubTimeStr));
		/* 时间 */
		menu.setCreateId(adminsSess.getId());
		menu.setCreateTime(new Date());
		menu.setUpdateTime(new Date());
		
		/* 存储数据 */
		JSONObject resDbJSON = this.authService.saveOneMenuService(menu);
		/* 结果 */
		this.info = resDbJSON.get("info") + "" ; 
		
		JSONObject resultJSON = printJuiJson(request,info);
		return resultJSON.toJSONString(); 
	}
	
	/**
	 * 打开菜单更新的页面
	 * 
	 * 查询你要更新的菜单
	 * 
	 * @return
	 */
	@RequestMapping("/menuUpdate")
	public String menuUpdate(HttpServletRequest request,String operType , String self,String rid,Model model)
	{
		ConstatFinalUtil.SYS_LOGGER.info("--menuUpdate--");
		
		Map<String, Object> condMap = new HashMap<String, Object>();
		/* 根据Id查询菜单 */
		condMap.put("id", rid);
		AMenu menu = this.authService.findOneMenuService(condMap);
		
		/* 用户自己修改 */
		if("true".equalsIgnoreCase(self))
		{
			menu = (AMenu) this.findObjfromSession(request, "2");
		}
		model.addAttribute("menu", menu);
		
		if("updateInfo".equalsIgnoreCase(operType))
		{
			return "/back/admins/menuUpdate";
		}
		return "/back/admins/menuInfo";
	}
	
	/**
	 * 菜单更新提交
	 * @return
	 */
	@RequestMapping(value = "/menuUpdateSubmit",produces="text/html;charset=UTF-8")
	@ResponseBody
	public String menuUpdateSubmit(HttpServletRequest request,AMenu menu,String operType,String pubTimeStr)
	{
		ConstatFinalUtil.SYS_LOGGER.info("--menuUpdateSubmit--");
		AAdmins adminsSess = (AAdmins) this.findObjfromSession(request, "2");
		/* 根据id从数据库中再查询一次 
		 * 一一的赋值*/
		Map<String, Object> condMap = new HashMap<String, Object>();
		/* 根据Id查询菜单 */
		condMap.put("id", menu.getId());
		AMenu menuRes = this.authService.findOneMenuService(condMap);
		
		boolean flag = false ; 
		if("updateInfo".equalsIgnoreCase(operType))
		{
			menuRes.setPubTime(this.dateFormatUtil.strDateTime(pubTimeStr));
			
			menuRes.setName(menu.getName());
			menuRes.setUrl(menu.getUrl());
			menuRes.setStatus(menu.getStatus());
			menuRes.setContent(menu.getContent());
			menuRes.setUpdateTime(new Date());
			
			menuRes.setUpdateId(adminsSess.getId());
			
			flag = true ; 
		}
		
		if(flag)
		{
			menuRes.setUpdateTime(new Date());
			/* 千万千万别使用Spring的对象驱动;在Controller方法中增加一个对象 */
			JSONObject resDbJSON = this.authService.updateOneMenuService(menuRes);
			this.info = resDbJSON.getString("info");
		}
		JSONObject resultJSON = this.printJuiJson(request, info);
		return resultJSON.toJSONString(); 
	}
	
	/**
	 * 菜单列表的树形结构
	 * @return
	 */
	@RequestMapping("/menuTreeList")
	public String menuTreeList(Model model,String rid)
	{
		ConstatFinalUtil.SYS_LOGGER.info("--menuTreeList--");
		if(rid == null)
		{
			rid = ""; 
		}
		/* 生成树形结构的字符串 
		String result = this.authService.proccedMenuTreeStr("menu","");
		 存储字符串 
		model.addAttribute("result", result);*/
		List<AMenu> menuTreeList = this.authService.proccedMenuTreeList("roleMenu", rid);
		model.addAttribute("menuTreeList", menuTreeList);
		return "/back/admins/menuTreeList" ; 
	}
	/*---- 菜单模块结束 ----*/
	
	/*---- 权限分配开始 ----*/
	/**
	 * 权限分配页面
	 * @return
	 */
	@RequestMapping("/roleMenuInsert")
	public String roleMenuInsert(String roleId,Model model)
	{
		ConstatFinalUtil.SYS_LOGGER.info("--roleMenuInsert--");
		/* 生成树形结构的字符串 
		String result = this.authService.proccedMenuTreeStr("roleMenu",roleId);
		 存储字符串 
		model.addAttribute("result", result);*/
		return "/back/admins/roleMenuInsert" ; 
	}
	
	/**
	 * 权限分配页面
	 * 
	 * 获取前台页面哪些复选框被选中;前台选择了就是数据中有数据,木有选择是null
	 * 往中间表(角色菜单表)增加数据,加数据之前,先删除老数据
	 * 提示信息:操作成功,总条数:{},成功:{},失败:{}
	 * @return
	 */
	@RequestMapping(value = "/roleMenuInsertSubmit",produces="text/html;charset=UTF-8")
	@ResponseBody
	public String roleMenuInsertSubmit(HttpServletRequest request,String[] menuId,String roleId)
	{
		ConstatFinalUtil.SYS_LOGGER.info("--roleMenuInsertSubmit--menuId:{}",Arrays.toString(menuId));
		
		/* 默认值 */
		this.info = "操作失败" ; 
		if(menuId != null)
		{
			int totalCount = menuId.length;
			int succedCount = 0; 
			int failedCount = 0;
			
			/*
			 * 删除老数据
			 * */
			Map<String,Object> condMap = new HashMap<String, Object>();
			condMap.put("roleId", roleId);
			this.authService.deleteOneRoleMenuService(condMap);
			
			for (int i = 0; i < menuId.length; i++)
			{
				String meId = menuId[i];
				
				
				try
				{
					/* 往中间表(角色菜单表)增加数据, */
					ARoleMenu roleMenu = new ARoleMenu() ; 
					roleMenu.setMenuId(Integer.valueOf(meId));
					roleMenu.setRoleId(Integer.valueOf(roleId));
					roleMenu.setCreateTime(new Date());
					roleMenu.setUpdateTime(new Date());
					roleMenu.setPubTime(new Date());
					JSONObject resultDbJson = this.authService.saveOneRoleMenuService(roleMenu);
					if("0".equalsIgnoreCase(resultDbJson.getString("code")))
					{
						succedCount ++ ; 
					}else
					{
						failedCount ++ ; 
					}
				} catch (Exception e)
				{
					failedCount ++ ; 
				}
			}
			
			/* 拼装提示信息 */
			this.info = "操作成功,总条数:" + totalCount + ",成功:" + succedCount + ",失败:" + failedCount;
		}
		
		ConstatFinalUtil.SYS_LOGGER.info("权限分配:信息:{},roleId:{},菜单:{}",this.info,roleId,Arrays.toString(menuId));
		JSONObject resultJSON = this.printJuiJson(request, info);
		return resultJSON.toJSONString(); 
	}
	/*---- 权限分配结束 ----*/
	
	/**
	 * 退出
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping("/logout")
	public String logout(HttpServletRequest request, HttpSession session,String returnUrl) throws Exception
	{
		ConstatFinalUtil.SYS_LOGGER.info("--logout--");
		AAdmins admins = (AAdmins) this.findObjfromSession(request, "2");
		
		/* 删除session中的信息 */
		session.removeAttribute("admins");
		session.removeAttribute("lastLoginTime");
		
		/* 删除token */
		this.usersOperService.removeAdminsTokenService(admins);
		
		/* 查找是否有returnUrl */
		if(returnUrl == null)
		{
			returnUrl = "" ; 
		}
		if(!"".equalsIgnoreCase(returnUrl))
		{
			return "redirect:/login.htm?returnUrl=" + URLEncoder.encode(returnUrl,"UTF-8");
		}
		
		return "redirect:/login.htm" ; 
	}
}
